package com.ljl.util;

import com.alibaba.fastjson.JSONObject;
import org.redisson.Redisson;
import org.redisson.api.RBuckets;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.redisson.client.codec.StringCodec;
import org.redisson.config.Config;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author ljl
 * @version 1.0
 * @date 2022/10/11 13:18
 */
public class RedisTool {

    /**
     * 批量保存redi数据
     * @param host 主机名
     * @param port 端口
     * @param password 密码
     * @param database 数据库
     * @param data 数据key-value
     */
    public static void saveAll(String host, int port, String password, int database, Map<String, ?> data) {
        if (data == null || data.size() == 0) {
            return;
        }
        Config config = new Config();
        config.setCodec(StringCodec.INSTANCE);
        config.useSingleServer()
                .setDatabase(database)
                .setPassword(Objects.equals(password, "") ? null : password)
                .setAddress("redis://" + host + ":" + port);
        RedissonClient client = Redisson.create(config);
        RBuckets buckets = client.getBuckets();
        buckets.set(data);
        client.shutdown();
    }

}
